/**
 * 习题标签 - 新增或编辑界面
 */
Ext.define("PSI.Drill.DrillTagEditForm", {
	extend : "PSI.AFX.BaseDialogForm",

	/**
	 * 初始化组件
	 */
	initComponent : function() {
		var me = this;

		var entity = me.getEntity();

		me.adding = entity == null;

		var buttons = [];
		if (!entity) {
			var btn = {
				text : "保存并继续新增",
				formBind : true,
				handler : function() {
					me.onOK(true);
				},
				scope : me
			};

			buttons.push(btn);
		}

		var btn = {
			text : "保存",
			formBind : true,
			iconCls : "PSI-button-ok",
			handler : function() {
				me.onOK(false);
			},
			scope : me
		};
		buttons.push(btn);

		var btn = {
			text : entity == null ? "关闭" : "取消",
			handler : function() {
				me.close();
			},
			scope : me
		};
		buttons.push(btn);

		var title = entity == null ? "新增习题标签" : "编辑习题标签";
		title = me.formatTitle(title);
		var iconCls = entity == null ? "PSI-button-add" : "PSI-button-edit";
		Ext.apply(me, {
			header : {
				title : title,
				height : 40,
				iconCls : iconCls
			},
			width : 400,
			height : 200,
			layout : "fit",
			listeners : {
				show : {
					fn : me.onWndShow,
					scope : me
				},
				close : {
					fn : me.onWndClose,
					scope : me
				}
			},
			items : [ {
				id : "PSI_Drill_DrillTagEditForm_editForm",
				xtype : "form",
				layout : {
					type : "table",
					columns : 1
				},
				height : "100%",
				bodyPadding : 5,
				defaultType : 'textfield',
				fieldDefaults : {
					labelWidth : 60,
					labelAlign : "right",
					labelSeparator : "",
					msgTarget : 'side',
					width : 370,
					margin : "5"
				},
				items : [ {
					xtype : "hidden",
					name : "id",
					value : entity == null ? null : entity.get("id")
				}, {
					id : "PSI_Drill_DrillTagEditForm_editName",
					fieldLabel : "标签名称",
					allowBlank : false,
					blankText : "没有输入标签名称",
					beforeLabelTextTpl : PSI.Const.REQUIRED,
					name : "name",
					value : entity == null ? null : entity.get("name"),
					listeners : {
						specialkey : {
							fn : me.onEditNameSpecialKey,
							scope : me
						}
					}
				}, {
					id : "PSI_Drill_DrillTagEditForm_editNote",
					fieldLabel : "备注",
					name : "note",
					value : entity == null ? null : entity.get("note"),
					listeners : {
						specialkey : {
							fn : me.onEditNoteSpecialKey,
							scope : me
						}
					}
				}, {
					id : "PSI_Drill_DrillTagEditForm_editShowOrder",
					fieldLabel : "显示次序",
					name : "showOrder",
					value : entity == null ? null : entity.get("showOrder"),
					listeners : {
						specialkey : {
							fn : me.onEditShowOrderSpecialKey,
							scope : me
						}
					}
				} ],
				buttons : buttons
			} ]
		});

		me.callParent(arguments);

		me.editForm = Ext.getCmp("PSI_Drill_DrillTagEditForm_editForm");

		me.editName = Ext.getCmp("PSI_Drill_DrillTagEditForm_editName");
		me.editNote = Ext.getCmp("PSI_Drill_DrillTagEditForm_editNote");
		me.editShowOrder = Ext
				.getCmp("PSI_Drill_DrillTagEditForm_editShowOrder");
	},

	/**
	 * 保存
	 */
	onOK : function(thenAdd) {
		var me = this;
		var f = me.editForm;
		var el = f.getEl();
		el.mask(PSI.Const.SAVING);
		var sf = {
			url : me.URL("/Home/Drill/editDrillTag"),
			method : "POST",
			success : function(form, action) {
				me.__lastId = action.result.id;

				el.unmask();

				PSI.MsgBox.tip("数据保存成功");
				me.focus();
				if (thenAdd) {
					me.clearEdit();
				} else {
					me.close();
				}
			},
			failure : function(form, action) {
				el.unmask();
				PSI.MsgBox.showInfo(action.result.msg, function() {
					me.editName.focus();
				});
			}
		};
		f.submit(sf);
	},

	onEditNameSpecialKey : function(field, e) {
		var me = this;

		if (e.getKey() == e.ENTER) {
			var editNote = me.editNote;
			editNote.focus();
			editNote.setValue(editNote.getValue());
		}
	},

	onEditNoteSpecialKey : function(field, e) {
		var me = this;

		if (e.getKey() == e.ENTER) {
			var editShowOrder = me.editShowOrder;
			editShowOrder.focus();
			editShowOrder.setValue(editShowOrder.getValue());
		}
	},

	onEditShowOrderSpecialKey : function(field, e) {
		var me = this;

		if (e.getKey() == e.ENTER) {
			var f = me.editForm;
			if (f.getForm().isValid()) {
				me.onOK(me.adding);
			}
		}
	},

	clearEdit : function() {
		var me = this;
		me.editName.focus();

		var editors = [ me.editName, me.editName, me.editShowOrder ];
		for (var i = 0; i < editors.length; i++) {
			var edit = editors[i];
			edit.setValue(null);
			edit.clearInvalid();
		}
	},

	onWindowBeforeUnload : function(e) {
		return (window.event.returnValue = e.returnValue = '确认离开当前页面？');
	},

	onWndClose : function() {
		var me = this;

		Ext.get(window).un('beforeunload', me.onWindowBeforeUnload);

		if (me.__lastId) {
			if (me.getParentForm()) {
				me.getParentForm().onRefresh();
			}
		}
	},

	onWndShow : function() {
		var me = this;
		var entity = me.getEntity();

		Ext.get(window).on('beforeunload', me.onWindowBeforeUnload);

		var editName = me.editName;
		editName.focus();
		editName.setValue(editName.getValue());

		if (!entity) {
			return;
		}

		me.getEl().mask("数据加载中...");
		Ext.Ajax.request({
			url : me.URL("/Home/Drill/tagInfo"),
			method : "POST",
			params : {
				id : entity.get("id")
			},
			callback : function(options, success, response) {
				me.getEl().unmask();
				if (success) {
					var data = Ext.JSON.decode(response.responseText);
					me.editName.setValue(data.name);
					me.editNote.setValue(data.note);
					me.editShowOrder.setValue(data.showOrder);
				}
			}
		});
	}
});